package com.sugon.modules.tools.service.impl;

import cn.hutool.core.util.StrUtil;
import com.sugon.consts.ToolsConsts;
import com.sugon.entity.ToolsCheckPlanEntity;
import com.sugon.modules.base.consts.SysApproveStatusConst;
import com.sugon.modules.base.model.dto.HandleApproveResult;
import com.sugon.modules.base.model.vo.ProcessProgressConst;
import com.sugon.modules.base.service.impl.AbstractErmsBaseApproveManager;
import com.sugon.modules.sys.consts.ProcessProgressStatus;
import com.sugon.modules.tools.service.IToolsCheckPlanManager;
import com.sugon.service.SysMsgService;
import com.sugon.service.SysProcessInfoService;
import com.sugon.service.ToolsCheckPlanItemService;
import com.sugon.service.ToolsCheckPlanService;
import com.sugon.utils.ProcessInfoEnum;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import javax.annotation.Resource;

/**
 * @author ： YuXD
 * @description： 工装精度检测Manager实现类
 * @date ： 2020-11-12 12:26
 * @version: 1.0
 */
@Service
public class ToolsCheckPlanManagerImpl extends AbstractErmsBaseApproveManager implements IToolsCheckPlanManager {

    @Resource
    private ToolsCheckPlanItemService toolsCheckPlanItemService;
    @Resource
    private ToolsCheckPlanService toolsCheckPlanService;
    @Resource
    private SysProcessInfoService processInfoService;
    @Resource
    private SysMsgService msgService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean newlyAdd(ToolsCheckPlanEntity toolsCheckPlan) {
        toolsCheckPlan.setStatus(ProcessProgressConst.NEW);
        // 设置产线ID
        String devLineId = toolsCheckPlan.getDevLineId();
        if (StrUtil.isNotEmpty(devLineId) && devLineId.length() > 4) {
            toolsCheckPlan.setWorkshop(devLineId.substring(0, 4));
        }
        ToolsCheckPlanEntity newestPlan = null;
        if (ToolsConsts.CHECK_PLAN_CONTINUE_USE.equals(toolsCheckPlan.getIsNewly())) {
            // 找出该产线最近的工装检测计划
            newestPlan = toolsCheckPlanService.findLastestPassedPlan(toolsCheckPlan.getId(), toolsCheckPlan.getDevLineId());
            if (newestPlan != null) {
                toolsCheckPlan.setContinueUsePlanId(newestPlan.getId());
            }
        }
        // 保存工装检测计划
        int saveFlag = toolsCheckPlanService.save(toolsCheckPlan);
        cn.hutool.core.lang.Assert.isTrue(saveFlag > 0);
        // 如果需要继续沿用最近一次相同产线已审核过的工装检测计划，则执行工装检测计划复制功能
        if (newestPlan != null) {
            toolsCheckPlanItemService.copy(newestPlan.getId(), toolsCheckPlan.getId());
        }
        return saveFlag > 0;
    }

    /**
     * 提交审核
     *
     * @param checkPlan 工装检测计划
     * @return
     */
    @Override
    public boolean commitApprove(ToolsCheckPlanEntity checkPlan) {
        checkPlan.setStatus(ProcessProgressConst.DEPT_APPROVING);
        boolean flag = toolsCheckPlanService.update(checkPlan) > 0;
        if (flag) {
            String checkPlanId = checkPlan.getId();
            // 保存流程信息
            processInfoService.saveProcessInfo(checkPlanId, ProcessInfoEnum.TLS_GZZQJCGL, 0, null);
            // 添加备件审批事项
            msgService.addToolsCheckPlanApproveItem(checkPlanId, 0);
        }
        return flag;
    }

    /**
     * 处理车间审核
     *
     * @param approveItemId 审批事项ID
     * @param approveStatus 审批状态
     * @param remark        审批备注
     * @return
     */
    @Override
    public HandleApproveResult handleWorkshopApprove(String approveItemId, Integer approveStatus, String remark) {
        HandleApproveResult handleApproveResult = new HandleApproveResult();
        ToolsCheckPlanEntity checkPlan = toolsCheckPlanService.queryObject(approveItemId);
        Assert.notNull(checkPlan, "工装周期性检测计划不存在，请确认！");
        // 校验规程进度
        Assert.isTrue(ProcessProgressConst.DEPT_APPROVING.equals(checkPlan.getStatus()), "只可以审核处于审核中的维护计划！");
        // 如果审核通过
        if (approveStatus == SysApproveStatusConst.PASSED) {
            checkPlan.setStatus(ProcessProgressConst.PASSED);
            int rows = toolsCheckPlanService.update(checkPlan);
            if (rows > 0) {
                handleApproveResult.setProcessProgressStatus(ProcessProgressStatus.END);
                handleApproveResult.setTitle(checkPlan.getPlanName());
                handleApproveResult.setNextNodeLevel(0);
            }
        } else if (approveStatus == SysApproveStatusConst.NOT_PASSED) {
            checkPlan.setStatus(ProcessProgressConst.NOT_PASSED);
            int rows = toolsCheckPlanService.update(checkPlan);
            if (rows > 0) {
                handleApproveResult.setProcessProgressStatus(ProcessProgressStatus.ING);
                handleApproveResult.setTitle(checkPlan.getPlanName());
                handleApproveResult.setUserId(checkPlan.getModifyUser());
            }
        }
        return handleApproveResult;
    }

}
